package cn.tradewin.advisor.pages.project;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

import cn.tradewin.advisor.commons.selectmodel.CodeLabelSelectModel;
import cn.tradewin.advisor.commons.util.CodeTableSymbols;
import cn.tradewin.advisor.commons.util.LanguageSymbols;
import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.persist.model.Project;
import cn.tradewin.advisor.persist.services.AccountService;
import cn.tradewin.advisor.persist.services.CodetableService;
import cn.tradewin.advisor.persist.services.CustomerService;
import cn.tradewin.advisor.persist.services.DeptService;
import cn.tradewin.advisor.persist.services.ProjectService;

public class ProjectAdd {

	@Property
	private Project project;

	@Inject
	private Logger log;

	@Inject
	private ProjectService projectService;
	
	@Property
	private SelectModel contractTypeModel;
	
	@Property
	private SelectModel customerModel;
	
	@Property
	private SelectModel deptModel;
	
	@Inject
	private CodetableService codetablelService;
	
	@Inject
	private CustomerService customerService;

	@Inject
	private DeptService deptService;
	
	@Inject
	private AccountService accountService;
	

	void onPrepareForRender() throws DBException {
		customerModel = new CodeLabelSelectModel(customerService.getAllCustomerOptions());
		deptModel = new CodeLabelSelectModel(deptService.getAllDeptsOptions());
		contractTypeModel = new CodeLabelSelectModel(codetablelService.listCategory(CodeTableSymbols.CONTRACT_TYPE, LanguageSymbols.ZH));
	}

	@Log
	void onPrepare() throws Exception {
		if (project == null) {
			project = new Project();
		}
	}

	@Log
	Object onSubmit() {
		try {
			projectService.addProject(project);
		} catch (Exception e) {
			log.info(e.getMessage());
		}
		return ProjectBoard.class;
	}
}
